#include "ring_buf.h"


int ring_buf_init(rint_buf_t *ring_buf)
{
	ring_buf->head = ring_buf->tail = 0;
	ring_buf->size = RINGBUF_SIZE;
	ring_buf->buf = (uint8_t *)malloc(sizeof(uint8_t)*RINGBUF_SIZE);
	if (ring_buf->buf == NULL)
	{
		return RINGBUF_MALLOC_FAILED;
	}

	return RINGBUF_SUCCESS;
	
}

int is_ring_buf_full(rint_buf_t *ring_buf)
{
	if (ring_buf == NULL)
	{
		return RINGBUF_NULL;
	}
	if ( ((ring_buf->tail + 1) % RINGBUF_SIZE) == ring_buf->head)
	{
		return RINGBUF_FULL;
	}
	else
	{
		return RINGBUF_NOT_FULL;
	}
}

int add_to_ring_buf(rint_buf_t *ring_buf, uint8_t data)
{
	if (ring_buf == NULL)
	{
		return RINGBUF_NULL;
	}
	if (is_ring_buf_full(ring_buf) == RINGBUF_NOT_FULL)
	{
		ring_buf->buf[ring_buf->tail] = data;
		ring_buf->tail = (ring_buf->tail + 1) % RINGBUF_SIZE;
		return RINGBUF_SUCCESS;
	}
	return RINGBUF_FAILED;
}

int is_ring_buf_empty(rint_buf_t *ring_buf)
{
	if (ring_buf == NULL)
	{
		return RINGBUF_NULL;
	}
	if (ring_buf->tail == ring_buf->head)
	{
		return RINGBUF_EMPTY;
	}
	else
	{
		return RINGBUF_NOT_EMPTY;
	}
}


int remove_from_ring_buf(rint_buf_t *ring_buf,uint8_t *data)
{
	if (ring_buf == NULL)
	{
		return RINGBUF_NULL;
	}
	if (is_ring_buf_empty(ring_buf) == RINGBUF_NOT_EMPTY)
	{
		*data = ring_buf->buf[ring_buf->head];
		ring_buf->head= (ring_buf->head + 1) % RINGBUF_SIZE;
		return RINGBUF_SUCCESS;
	}
	return RINGBUF_FAILED;
}



